package top.lilele.adminSystem.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.lilele.adminSystem.exception.account.*;
import top.lilele.adminSystem.exception.business.*;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.ResponseCodeEnum;

import java.util.List;

/**
 * 全局异常捕获
 *
 * @author lilele
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public BaseResponse exceptionHandler(Exception e) {
        e.printStackTrace();
        //参数异常
        if (e instanceof MethodArgumentNotValidException) {
            log.error("【全局异常拦截】前端入参异常：请求错误信息 {}", e.getMessage());
            MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e;
            List<ObjectError> errors = methodArgumentNotValidException.getBindingResult().getAllErrors();
            StringBuilder stringBuilder = new StringBuilder();
            errors.stream().forEach(res -> {
                stringBuilder.append(res.getDefaultMessage()).append(",");
            });
            return BaseResponse.baseInfo(999, stringBuilder.substring(0, stringBuilder.length() - 1), null);
        }
        //自定义账号注册相关失败异常
        else if (e instanceof AccountWrongException) {
            log.error("【全局异常拦截】登录异常：请求错误信息 {}", e.getMessage());
            return BaseResponse.info(ResponseCodeEnum.AccountWrong, null);
        } else if (e instanceof AccountDisableException) {
            log.error("【全局异常拦截】登录异常：请求错误信息 {}", e.getMessage());
            return BaseResponse.info(ResponseCodeEnum.AccountDisable, null);
        } else if (e instanceof CodeOutTimeException) {
            return BaseResponse.info(ResponseCodeEnum.CodeOutTime, null);
        } else if (e instanceof CodeErrorException) {
            return BaseResponse.info(ResponseCodeEnum.CodeError, null);
        } else if (e instanceof DepartmentHasChildrenException) {
            return BaseResponse.info(ResponseCodeEnum.HasChildrenDept, null);
        } else if (e instanceof LoseIcoException) {
            return BaseResponse.info(ResponseCodeEnum.LoseIco, null);
        } else if (e instanceof FileUploadOverMaxSizeException) {
            return BaseResponse.info(ResponseCodeEnum.FileUploadOverMaxSize, null);
        } else if (e instanceof FileNotExistException) {
            return BaseResponse.info(ResponseCodeEnum.FileNotExist, null);
        } else if (e instanceof PasswordErrorException) {
            return BaseResponse.info(ResponseCodeEnum.PasswordError, null);
        } else if (e instanceof PasswordNotEqualsException) {
            return BaseResponse.info(ResponseCodeEnum.PasswordNotEquals, null);
        } else if (e instanceof DailyHasFilledException) {
            return BaseResponse.info(ResponseCodeEnum.DailyHasFilled, null);
        }
        // sql约束重复
        else if (e instanceof DuplicateKeyException) {
            return BaseResponse.info(ResponseCodeEnum.SQLIntegrityConstraintViolationException, null);
        }
        //sa-token异常
        //未登录或登录信息已过期
        else if (e instanceof NotLoginException) {
            log.error("【全局异常拦截】未登录或登录信息已过期异常，请求错误信息 {}", e.getMessage());
            return BaseResponse.info(ResponseCodeEnum.NoLogin, null);
        }
        //无权限异常
        else if (e instanceof NotPermissionException) {
            log.error("【全局异常拦截】无权限异常，请求错误信息 {}", e.getMessage());
            return BaseResponse.info(ResponseCodeEnum.NoPermission, null);
        }
        //全局异常
        log.error("【全局异常拦截】拦截所有异常：请求错误信息 {}", e.getMessage());
        return BaseResponse.info(ResponseCodeEnum.Fail, null);
    }
}
